# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=rfcat
pkgver=v2.0.1.r5.g07f99bb
pkgrel=5
epoch=1
pkgdesc='Swiss-army knife of ISM band radio.'
arch=('any')
groups=('blackarch' 'blackarch-radio' 'blackarch-exploitation')
url='https://code.google.com/archive/p/rfcat/'
license=('BSD-3-Clause' 'GPL-2.0-or-later')
depends=('python' 'python-future' 'python-pyusb' 'pyside6' 'libusb')
makedepends=('git' 'python-setuptools')
source=("git+https://github.com/atlas0fd00m/$pkgname.git")
sha512sums=('SKIP')

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

prepare() {
  cd "$pkgname"

  # Exclude tests from package data
  python - <<'PY'
from pathlib import Path
p = Path("setup.py")
s = p.read_text()
if "exclude_package_data" not in s:
    s = s.replace(
        "package_data     = pkgdata,",
        'package_data     = pkgdata,\n        exclude_package_data={"": ["tests/*", "*/tests/*"]},'
    )
# Ensure include_package_data isn't True
s = s.replace("include_package_data = True", "include_package_data = False")
p.write_text(s)
PY

  # Replace PySide2 with PySide6
  sed -i 's/from PySide2/from PySide6/g' rflib/ccspecan.py
  sed -i 's/\.exec_(/.exec(/g' rflib/ccspecan.py
  sed -i 's/\.exec_(/\.exec(/g' rflib/__init__.py
}

build() {
  cd $pkgname

  python setup.py build
}

package() {
  cd $pkgname

  python setup.py install --root="$pkgdir" --prefix=/usr -O1 --skip-build

  for i in "$pkgdir/usr/bin/"*_*; do
    mv $i "$(echo $i | tr -s '_' '-')"
  done
}

